<template>
  <div class="search-box">
    <i class="icon icon-search">&#xe638;</i>
    <input
      type="text"
      class="box"
      :placeholder="placeholder"
      v-model="inputValue"
    />
    <i class="icon icon-dismiss" v-show="inputValue" @click="clear">&#xe656;</i>
  </div>
</template>

<script>
import { debounce } from "../common/js/utils";
export default {
  props: {
    placeholder: {
      type: String,
      default: "搜索歌曲、歌手",
    },
    query: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      inputValue: "",
    };
  },
  created: function () {
    this.$watch(
      "inputValue",
      debounce(() => {
        this.$emit("query", this.inputValue);
      }, 1000)
    );
  },
  watch: {
    query: function () {
      if (this.query) {
        this.inputValue = this.query;
      }
    },
  },
  methods: {
    clear() {
      this.inputValue = "";
    },
  },
};
</script>
<style lang="less" scoped>
@import "../assets/css/function.less";
.search-box {
  position: relative;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  width: 100%;
  .px2rem(padding-left, 30);
  .px2rem(padding-right, 30);
  .px2rem(height, 74);
  background: rgba(0, 43, 59, 0.9);

  border-radius: 6px;
  margin: 10px 0;
  .icon-search {
    font-size: 24px;
    color: #6b6a6a;
    position: absolute;
    left: 24px;
  }
  .box {
    flex: 1;
    margin: 0 5px;
    .px2rem(line-height, 76);
    padding: 0 30px;
    background: rgba(5, 63, 90);

    color: #fff;
    font-size: 14px;
    outline: 0;
    border: none;
    border-radius: 6px;
    &::placeholder {
      color: hsla(0, 0%, 100%, 0.3);
    }
  }
  .icon-dismiss {
    font-size: 20px;
    .px2rem(margin-right, 10);
    color: #6b6a6a;
    position: absolute;
    right: 20px;
  }
}
</style>